<?php
namespace app\index\controller;

use Firebase\JWT\JWT;
use think\Request;
use think\Controller;
class Index extends Controller {
    // 定义中间件检测检测token
//    protected $middleware = [
//        'CheckToken'=> ['only'    => ['index'] ],
//        'Log'
//    ];

    public function index(Request $request)
    {
//        var_dump($request->user_id);die();
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改（2006-2018） - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }


    // 生成token
    function createToken(){
        $user_id = 101;
        $secret = config('jwt.secret');
        $payload = [
            'iss'=>'sol',                //签发人(官方字段:非必需)
            'exp'=>time()+3600*24*7,     //过期时间(官方字段:非必需)
            'aud'=>'adminSystem',        //受众(官方字段:非必需)
            'nbf'=>time(),               //生效时间(官方字段:非必需)
            'iat'=>time(),               //签发时间(官方字段:非必需)
            'user_id'=>$user_id,         //自定义字段
        ];
        $token = JWT::encode($payload,$secret,'HS256');
        return $token;
    }

    // 获取token的时候就判断token是否失效
    public function getUserIdByToken(Request $request){
        $get = $request->get();
        $token = $get['token']??'';
        if($token=='') exit('token 不能为空');
        $secret = config('jwt.secret');
        $all = JWT::decode($token,$secret,['typ'=>'JWT','alg'=>'HS256']);
        print_r($all);die();
    }


}
